<template>
	<view style="line-height: 1;overflow-y: hidden;">
		<!-- #ifndef APP-PLUS -->
			<video :src="video.src" :poster="video.poster" style="height: 225px;width: 750rpx;" controls></video>
		<!-- #endif -->
		<view class="movie-list-text">
			<text>资源列表</text>
		</view>
		<scroll-view scroll-y="true" :style="'height:' + scrollH + 'px;'">
			<view class="movie_list">
				<view v-for="(item, index) in jiList" :key="index">
				    <view :class="index === video.nowNum ? jiBlueList : jiGreyList" @click="playClick(item)" class="ji-blue-list">
						<text>{{ item.playName }}</text>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data () {
			return {
				video: {
					src: 'https://dow.dowlz17.com/20241023/6869_fb1fd66b/剑来-13.mp4',
					poster: 'https://img.y81s.com/upload/images/202408/b5f1c987110640d8edb36af335d47151.jpg',
					nowNum: 0,
					movieTitle: '',
					videoSeekTime: 0,
					movieName: ''
				},
				scrollH: 500,
				jiList: [],
				videoData: {},
				type: 1,
				jiBlueList: 'ji_blue_list',
				jiGreyList: 'ji_list',
				nowMovieData: {}
			}
		},
		onLoad(options) {
			const item = JSON.parse(decodeURIComponent(options.params));
			// uni.$emit('movieData', item);
			this.videoData = item;
			if (this.videoData.userHistoryId) {
				// 通过影视历史播放ID查询
				this.type = 1
				this.queryByUserHistoryId(this.videoData.userHistoryId)
			} else {
				// 通过影视详细地址去得到影视的集数
				this.type = 0
				this.queryMovieJi()
			}
			let res = uni.getSystemInfoSync();
			this.scrollH = res.windowHeight - 226;
		},
		onBackPress(e) {
			this.updateUserPlayer();
			if (this.type === 1) {
				uni.switchTab({
					url: '/pages/movies/index'
				});
			} else {
				// uni.navigateBack({
				//     delta: 1
				// });
			}
		},
		onHide() {
			this.updateUserPlayer();
		},
		methods: {
			// 如果用户返回了或者退出APP时需要记录当前播放到第几集和第几秒
			updateUserPlayer() {
				if(this.type === 1) {
					uni.showLoading({
						title: '更新中,请耐心等待一下!',
						mask: true
					})
					const _that = this
					if (!_that.nowMovieData.movieNowSecond) {
						_that.nowMovieData.movieNowSecond = 0;
					}
					// 调用后台接口
					_that.$api.post({
						url: '/app/movie/search/updateUserHistoryPlayer',
						params: _that.nowMovieData
					}).then(res => {
						// console.log('res', res)
						if (res && res.data.code === 200) {
							uni.showToast({
								title: '更新成功!',
								icon: 'none',
								duration: 2000
							})
						}
						uni.hideLoading()
					}).catch(error => {
						console.log('fail:', error)
						uni.hideLoading()
					})
				}
			},
			queryByUserHistoryId(userHistoryId) {
				uni.showLoading({
					title: '查询中,请耐心等待一下!',
					mask: true
				})
				const _that = this
				// 调用后台接口
				_that.$api.get({
					url: '/app/movie/search/searchByUserHistoryId',
					params: {
						userHistoryId: userHistoryId
					}
				}).then(res => {
					// console.log('res', res)
					if (res && res.data.code === 200) {
						let info = res.data.info
						_that.jiList = info.jiList
						// 获取当前播放到第几集
						var nowNumIndex = info.jiList.map(item => item.id).indexOf(info.movieNowJi)
						_that.video.nowNum = nowNumIndex;
						_that.video.src = info.movieJiUrl;
						_that.video.poster = 'https:' + info.movieImgUrl;
						_that.video.movieTitle = '';
						_that.video.movieTitle = info.movieName + ' - ' + info.playName;
						_that.video.movieName = info.movieName;
						if (info.movieNowSecond) {
							_that.video.videoSeekTime = info.movieNowSecond;
						}
						_that.nowMovieData.movieJiUrl = info.movieJiUrl;
						// 传递播放参数
						let datas = {
							movieSrc: _that.video.src,
							moviePoster: _that.video.poster,
							movieTitle: _that.video.movieTitle
						};
						uni.setStorageSync('movie-now-play', datas);
					}
					uni.hideLoading()
				}).catch(error => {
					console.log('fail:', error)
					uni.hideLoading()
				})
			},
			queryMovieJi() {
				uni.showLoading({
					title: '查询中,请耐心等待一下!',
					mask: true
				})
				const _that = this
				// 调用后台接口
				_that.$api.post({
					url: '/app/movie/search/searchJi',
					params: _that.videoData
				}).then(res => {
					// console.log('res', res)
					if (res && res.data.code === 200) {
						_that.jiList = res.data.jiList
						// console.log(_that.movieDatas)
						_that.nowMovieData.movieJiUrl = _that.jiList[0].playUrl;
					}
					uni.hideLoading()
				}).catch(error => {
					console.log('fail:', error)
					uni.hideLoading()
				})
			},
			playClick(item) {
				uni.showLoading({
					title: '查询中,请耐心等待一下!',
					mask: true
				})
				// console.log(item)
				let datas = {};
				if (this.type === 0) {
					this.video.nowNum = item.index;
					datas = {
						movieSrc: item.playUrl,
						moviePoster: this.video.poster,
						movieTitle: this.videoData.movieName + ' - ' + item.playName,
						videoSeekTime: 0
					};
					this.nowMovieData.movieJiUrl = item.playUrl;
				} else {
					var nowNumIndex = this.jiList.map(data => data.id).indexOf(item.id)
					this.video.nowNum = nowNumIndex;
					datas = {
						movieSrc: item.movieJiUrl,
						moviePoster: this.video.poster,
						movieTitle: this.video.movieName + ' - ' + item.playName,
						videoSeekTime: 0
					};
					this.nowMovieData.movieJiUrl = item.movieJiUrl;
				}
				uni.$emit('movieData', datas);
				uni.hideLoading()
			}
		}
	}
</script>

<style lang="scss">
	.movie-list-text {
		margin: 20rpx 0 20rpx 20rpx;
	}
	
	.movie_list {
		display: flex;
		flex-wrap: wrap;
	}
	
	.ji-blue-list {
		width: 150rpx;
		height: 60rpx;
		background: #0000ff;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-left: 10rpx;
		color: #ffffff;
		border-radius: 10rpx;
		flex-basis: 25%;
		box-sizing: border-box;
		margin: 15rpx 20rpx 0 15rpx;
	}
	.ji_list {
		width: 150rpx;
		height: 60rpx;
		background: #636363;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-left: 10rpx;
		color: #ffffff;
		border-radius: 10rpx;
		flex-basis: 25%;
		box-sizing: border-box;
		margin: 15rpx 20rpx 0 15rpx;
	}
	
</style>